﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using Gma.UserActivityMonitor;



namespace WpfXamlPopup
{
    public enum MsgType { Error, Warning, Info };
    /// <summary>
    /// Interaction logic for debug.xaml
    /// </summary>
    public partial class debug : Window
    {

        private delegate void TraceDbgDelegate(string message, string details, MsgType tip);

        public debug()
        {
            InitializeComponent();
            richTextBox_debug.IsDocumentEnabled = true;
            TextBoxOutputter outputter = new TextBoxOutputter(this);
            TextBoxOutputterErr outputterErr = new TextBoxOutputterErr(this);
            Console.SetOut(outputter);
            Console.SetError(outputterErr);
        }

        public void TraceDbg(string message, string details, string tip)
        {
            MsgType msgType;
            switch (tip)
            {
                case "Error":
                    msgType = MsgType.Error;
                    break;
                case "Warning":
                    msgType = MsgType.Warning;
                    break;
                default:
                    msgType = MsgType.Info;
                    break;
            }
            TraceDbg(message, details, msgType);
        }

        public void TraceDbg(string message, string details, MsgType tip)
        {
            if (Dispatcher.CheckAccess())
            {
                Paragraph myParagraph = new Paragraph();
                if (details == null)
                {
                    var run = new Run(message);
                    run.Style = (Style)(Resources[tip.ToString()]);
                    myParagraph.Inlines.Add(run);
                   
                }
                else
                {
                    Expander exp1 = new Expander();
                    exp1.Content = details;
                    exp1.Header = message;
                    exp1.Style = (Style)(Resources[tip.ToString() + "Expander"]);
                    myParagraph.Inlines.Add(exp1);
                }
                richTextBox_debug.Document.Blocks.Add(myParagraph);
                if (!richTextBox_debug.IsMouseOver)
                    richTextBox_debug.ScrollToEnd();
            }
            else
            {
                Dispatcher.Invoke(new TraceDbgDelegate(TraceDbg), message, details, tip);
            }
        }

        public void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
        }

        private void checkBox_keyhooker_Checked(object sender, RoutedEventArgs e)
        {
            HookManager.KeyPress += new System.Windows.Forms.KeyPressEventHandler(HookManager_KeyPress);
        }

        private void checkBox_keyhooker_Unchecked(object sender, RoutedEventArgs e)
        {
            HookManager.KeyPress -= new System.Windows.Forms.KeyPressEventHandler(HookManager_KeyPress);
        }

        void HookManager_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            textBox_logging.AppendText(e.KeyChar.ToString());
        }



    }

    public class TextBoxOutputterErr : TextWriter
    {
        debug textBox = null;

        public TextBoxOutputterErr(debug output)
        {
            textBox = output;
        }

        public override void Write(string value)
        {
            base.Write(value);
            textBox.Dispatcher.BeginInvoke(new Action(() =>
            {
                textBox.TraceDbg(value, null, MsgType.Error);
            }));
        }

        public override Encoding Encoding
        {
            get { return System.Text.Encoding.ASCII; }
        }
    }

    public class TextBoxOutputter : TextWriter
    {
        debug textBox = null;

        public TextBoxOutputter(debug output)
        {
            textBox = output;
        }

        public override void Write(string value)
        {
            base.Write(value);
            textBox.Dispatcher.BeginInvoke(new Action(() =>
            {
                textBox.TraceDbg(value, null, MsgType.Info);
            }));
        }

        public override Encoding Encoding
        {
            get { return System.Text.Encoding.ASCII; }
        }
    }

}
